package com.bkgy.modular.tbPdiQualityRecordC642.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.tbPdiQualityRecordC642.entity.LedgerPlan;
import com.bkgy.modular.tbPdiQualityRecordC642.mapper.LedgerPlanMapper;
import com.bkgy.modular.tbPdiQualityRecordC642.service.LedgerPlanService;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.ExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @ClassName LedgerPlanServiceIMpl
 * @date 2023/6/13 10:12
 */
@Service
public class LedgerPlanServiceImpl extends ServiceImpl<LedgerPlanMapper, LedgerPlan> implements LedgerPlanService {

    @Resource
    private LedgerPlanMapper ledgerPlanMapper;

    @Override
    public ResultData findByLedgerPlanData(JSONObject jsonObject) {
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        JSONObject condition = jsonObject.getJSONObject("condition");

        if (null == pageIndex || null == pageSize) {
            return ResultData.ResultDataFaultSelf("分页参数没有传", null);
        }
        List<LedgerPlan> ledgerPlanList = ledgerPlanMapper.findByLedgerPlanData(
                (pageIndex - 1) * pageSize, pageSize * pageIndex,condition);
        Integer count = ledgerPlanMapper.findByLedgerPlanDataCount(condition);
        return ResultData.ResultDataSuccess(ledgerPlanList,count);
    }

    @Override
    public void export(JSONObject jsonObject, HttpServletResponse response) {
        // 接收传入参数
        Integer pageSize = jsonObject.getInteger("pageSize");
        Integer pageCount = jsonObject.getInteger("pageCount");
        String fileNames = jsonObject.getString("fileName");
        JSONObject condition = jsonObject.getJSONObject("condition");
        // 创建表格数据集合
        List<LedgerPlan> listExcel = new ArrayList<>();// 调用service查询出数据
        List<LedgerPlan> data = null;

        for (int i = 1; i <= pageCount; i++) {
            // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
            data = ledgerPlanMapper.findByLedgerPlanData((i - 1) * pageSize, i * pageSize,condition);
            if (!data.isEmpty() && data.size() > 0) {
                for (LedgerPlan item : data) {
                    listExcel.add(item);
                }
            }
        }
        if (CollectionUtils.isEmpty(listExcel)) {
            return;
        }
        ExcelUtils.downloadExcel(fileNames,response,LedgerPlan.class,listExcel);
    }

    @Override
    public  List<LedgerPlan> findByEntidData(List<String> entids) {
        if (CollectionUtils.isEmpty(entids)) {
            LambdaQueryWrapper<LedgerPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.isNotNull(LedgerPlan::getEntid);
            List<LedgerPlan> ledgerPlanList = ledgerPlanMapper.selectList(queryWrapper);
            return ledgerPlanList;
        } else {
            LambdaQueryWrapper<LedgerPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(LedgerPlan::getEntid, entids);
            List<LedgerPlan> ledgerPlanList = ledgerPlanMapper.selectList(queryWrapper);
            return ledgerPlanList;
        }
    }
}
